#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>

using namespace std;

struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
    
};

ListNode* mergeKLists(vector<ListNode*>& lists) {
    return mergeSort(lists, 0, lists.size() - 1);
}

ListNode* mergeSort(vector<ListNode*>& lists, int left, int right)
{
    if (left > right)    return nullptr;
    if (left == right)   return lists[left];

    int mid = (left + right) >> 1;
    ListNode* l1 = mergeSort(lists, left, mid);
    ListNode* l2 = mergeSort(lists, mid + 1, right);

    return mergeToList(l1, l2);

}

ListNode* mergeToList(ListNode* l1, ListNode* l2)
{
    if (l1 == nullptr)   return l2;
    if (l2 == nullptr)   return l1;

    ListNode head;
    head.next = nullptr;

    ListNode* tail = &head;
    ListNode* cur1 = l1, * cur2 = l2;

    while (cur1 && cur2)
    {
        if (cur1->val <= cur2->val)
        {
            tail->next = cur1;
            tail = tail->next;
            cur1 = cur1->next;
        }
        else
        {
            tail->next = cur2;
            tail = tail->next;
            cur2 = cur2->next;
        }

    }

    if (cur1 != nullptr) tail->next = cur1;
    if (cur2 != nullptr) tail->next = cur2;


    return head.next;
}

int main() {

	
	return 0;
}